using Services;
using UnityEngine;

[RequireComponent(typeof(GridTransform))]
public class Obstacle : MonoBehaviour, IGridTransform
{
    private ActionController actionController;
    private GridTransform gridTransform;
    public GridTransform GridTransform => gridTransform;

    public bool b_unbreakable;
    [SerializeField]
    protected int durability;

    protected int Durability
    {
        get => durability;
        set
        {
            durability = value;
            OnDurabilityChange(value);
        }
    }

    private void Awake()
    {
        actionController = ServiceLocator.Get<ActionController>();
        gridTransform = GetComponent<GridTransform>();
        gridTransform.Entrust(actionController.ObstacleManager, this);
    }

    public void Break(float power)
    {
        if (!b_unbreakable)
            Durability -= (int)power;
    }

    protected virtual void OnDurabilityChange(int value)
    {
        if (value <= 0)
            Destroy(gameObject);
    }
}
